package com.yb.bluetooth.util;

import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.provider.Settings;

import androidx.core.location.LocationManagerCompat;
import androidx.core.util.Consumer;
import androidx.fragment.app.FragmentActivity;

/**
 * Created by zyb
 * Date 2022/1/27 17:06
 * Desc
 */

public class LocationUtils {

    private static final int CODE_LOCATION_ENABLED = 0x2;

    /**
     * 检查是否打开定位
     *
     * @param context 上下文
     */
    public static boolean isLocationEnabled(Context context) {
        LocationManager manager = (LocationManager) context.getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
        return LocationManagerCompat.isLocationEnabled(manager);
    }

    /**
     * 打开位置信息
     *
     * @param activity   实例
     * @param callResult 结果回调
     */
    public static void openLocation(FragmentActivity activity, Consumer<Boolean> callResult) {
        if (isLocationEnabled(activity)) {
            callResult.accept(true);
        } else {
            ActivityCallUtils instance = ActivityCallUtils.instance(activity);
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            instance.startActivityForResult(intent, CODE_LOCATION_ENABLED);
            instance.callActivityResult(CODE_LOCATION_ENABLED, (resultCode, data) -> {
                callResult.accept(isLocationEnabled(activity));
            });
        }
    }

}


